package com.fortune.rainshinecommon.util;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;

/**
 * @program: rainShine
 * @description:
 * @author: tangyn
 * @create: 2019-06-04
 **/
public class PicUtils {

    public static byte[] quality(byte[] srcImgData, float scale) throws IOException {
        BufferedImage bi = ImageIO.read(new ByteArrayInputStream(srcImgData));
//        int widthOld = (int) bi.getWidth(); // 源图宽度

//        if(widthOld <= 880 && widthOld >= 300) {
//        	scale = 0.5f;
//        }else if(widthOld < 300) {
//        	scale = 1f;
//        }
        scale = 1f;
        int width = (int) (bi.getWidth() * scale); // 源图宽度
        int height = (int) (bi.getHeight() * scale); // 源图高度

        Image image = bi.getScaledInstance(width, height, Image.SCALE_SMOOTH);
        BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        Graphics g = tag.getGraphics();
        g.setColor(Color.RED);
        g.drawImage(image, 0, 0, null); // 绘制处理后的图
        g.dispose();

        java.io.ByteArrayOutputStream bOut = new java.io.ByteArrayOutputStream();
        ImageIO.write(tag, "JPEG", bOut);

        return bOut.toByteArray();
    }
}
